#include "PktParser.h"
#include <stdio.h>

PktParser::PktParser()
{}

int PktParser::parsePkt(Packet* pkt)
{
	switch(pkt->state)
	{
		case STATE_EthernetII:
			if(0 > aEthernetII.parse(pkt))
			{
				pkt->state = STATE_FAIL;
				return -1;
			}
			break;
		case STATE_IP:
			if(0 > aIP.parse(pkt))
			{
				pkt->state = STATE_FAIL;
				return -1;
			}
			break;
		case STATE_GRE:
			if(0 > aGRE.parse(pkt))
			{
				pkt->state = STATE_FAIL;
				return -1;
			}
			break;
		case STATE_TCP:
			if(0 > aTCP.parse(pkt))
			{
				pkt->state = STATE_FAIL;
				return -1;
			}
			break;
		case STATE_UDP:
			break;
		case STATE_HTTP:
			if(0 > aHTTP.parse(pkt))
			{
				pkt->state = STATE_FAIL;
				return -1;
			}
			break;
		case STATE_HTML:
			if(0 > aHTML.parse(pkt))
			{
				pkt->state = STATE_FAIL;
				return -1;
			}
			break;
		case STATE_FIN:
			return 0;
			break;
		case STATE_FAIL:		//parse fail
			return -1;
			break;
		default:
			return -1;		//protocol not supported
			break;
	}

	if(STATE_FIN != pkt->state)
	{
		if(-1 == parsePkt(pkt))
			return -1;
	}

	return 0;
}
